import '../css/style.scss';
import {loginStatus,queryOrder_new,qrcode,log} from './urlconfig/config.js';

var token;
var product_token='bb6d01050605000fba16597e6dc5ae28';
if(localStorage.token != null){
    token=localStorage.token
}
var edition = localStorage.getItem("edition");
var channel = localStorage.getItem("channel");
var sys = localStorage.getItem("sys");
var cpuid = localStorage.getItem("cpuid");
ewm();
$(function(){
    $(".pay-center  .office-tab").click(function () {
        var id = $(this).attr("data-id");
        product_token=$(this).attr("data-text");
        $(".pay-ewm").hide();

        var tablog=$(this).attr("data-log");
        tab(tablog);
        if(id > 0){
            $('.tab-show').css('display','none');
            $("#item-" + id).css('display','block');
            clearInterval(interval2);
            clearTimeout(time);
            ewm ()
        }
        if($(this).hasClass("office-person")){
            $(this).removeClass('active-personal-out');
            $(this).addClass('active-personal');
            $(this).siblings().removeClass('active-family');
            $(this).siblings().addClass('active-family-out');
        }else if($(this).hasClass("office-family")){
            $(this).removeClass('active-family-out');
            $(this).addClass('active-family');
            $(this).siblings().removeClass('active-personal');
            $(this).siblings().addClass('active-personal-out');
        }else{

        }
    });
});
/*切换日志*/
function tab(name){
    $.ajax({
        type:'GET',
        url:log+ name+'/'+ channel+'/'+ edition + '/'+sys+'/'+cpuid,
        dataType: 'json',
        success:function(res) {
            if(res.code==200){
                console.log(res.msg)
            }
        },
        error:function(err) {
            console.log(err)
        }
    });
}
var interval2;

function ewm (){
    $.ajax({
        type:'post',
        url:qrcode,
        dataType:'json',
        data:{
            token:token,
            product_token:product_token,
            channel:'plan_main',
            buy_position:'downloader'
        },
        success:function (res){
            if(res.code === 200) {
                clearTimeout(time);
                countdown=60;
                settime();
                $(".ewm").remove();
                var $img= '<img src="'+res.data.url+'" class="ewm" ondragstart="return false;">';
                $('.pay-bottom-left').append($img);
                pay();
            }
        },
        error:function (err){
            console.log(err)
        }
    });
}
function pay(){
    interval2 = setInterval(function () {
        $.ajax({
            type:'post',
            url:queryOrder_new,
            dataType:'json',
            data:{
                token:token
            },
            success:function (res){
                if(res.code === 200) {
                    if(res.data.product_title == 'office365_family'){
                        tab('officeFamily_wechat')
                    }else if(res.data.product_title == 'office365_personal'){
                        tab('officePersonal_wechat')
                    }else{

                    }
                    window.location.href="/key.html";
                    clearInterval(interval2);
                }
            },
            error:function (err){
                console.log(err)
            }
        })
    },2000)
}
/*二维码过期*/
var countdown=900;
var time;
function settime() {
    if (countdown == 0) {
        countdown = 900;
        $(".pay-ewm").show();
        clearInterval(interval2);
    } else {
        countdown--;
        console.log(countdown);
        time=setTimeout(function() {
            settime();
        },1000)
    }

}

/*重新请求二维码*/
$(".pay-ewm").click(function (){
    $(this).hide();
    ewm();
});
/*kefu日志*/


$(".logclick").click(function (){
    var tablog=$(this).attr("data-log");
    tab(tablog);
    if('Widget' in window){
        Widget.slotOpenExternalBrowser('http://office.zhuazi.com/kefu.html');
    }
});
$('.pay-title span').click(function (){
    var tablog=$(this).attr("data-log");
    tab(tablog);
})
function tab(name){
    $.ajax({
        type:'GET',
        url:log+ name+'/'+ channel+'/'+ edition + '/'+sys+'/'+cpuid,
        dataType: 'json',
        success:function(res) {
            if(res.code==200){
                console.log(res.msg)
            }
        },
        error:function(err) {
            console.log(err)
        }
    });
}
$(".family_all").click(function (){
    if('Widget' in window){
        Widget.slotOpenExternalBrowser('http://web.install.office.zhuazi.com/family.html');
    }
});
$(".personal_all").click(function (){
    if('Widget' in window){
        Widget.slotOpenExternalBrowser('http://web.install.office.zhuazi.com/personal.html');
    }
});
import './common.js';